package com.ly.share.juc.cas;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author icanci
 * @since 1.0 Created in 2022/06/05 22:45
 */
public class AbaDemo1 {
    private static AtomicInteger atomicInteger = new AtomicInteger(1);

    public static void main(String[] args) {

        new Thread(() -> {

            int expNum = atomicInteger.get();
            int newNum = expNum + 1;
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            boolean res = atomicInteger.compareAndSet(expNum, newNum);
            System.out.println(Thread.currentThread().getName() + " " + res);
        }, "t1").start();

        new Thread(() -> {
            try {
                Thread.sleep(20);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            int incre = atomicInteger.incrementAndGet();
            int decre = atomicInteger.decrementAndGet();

            System.out.println(Thread.currentThread().getName() + " incre: " + incre + ",decre: " + decre);
        }, "t1").start();
    }
}
